import { localCache } from "@/utils/cache";
import { LOGIN_TOKEN } from "@/global/constants";
import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import { firstMenu } from "@/utils/map-menus";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      redirect: "/main",
    },
    {
      path: "/login",
      component: () => import("../views/login/Login.vue"),
    },
    {
      path: "/main",
      name: "main",
      component: () => import("../views/main/Main.vue"),
    },
    {
      path: "/:pathMatch(.*)",
      component: () => import("../views/not-found/NotFound.vue"),
    },
  ],
});

// 1.获取所有的路由
// const localRoutes = [
//   {
//     path: "/main/analysis/overview",
//     component: () => import("@/views/main/analysis/overview/overview.vue"),
//   },
//   {
//     path: "/main/analysis/dashboard",
//     component: () => import("@/views/main/analysis/dashboard/dashboard.vue"),
//   },
//   {
//     path: "/main/system/user",
//     component: () => import("@/views/main/system/user/user.vue"),
//   },
//   {
//     path: "/main/system/department",
//     component: () => import("@/views/main/system/department/department.vue"),
//   },
//   {
//     path: "/main/system/menu",
//     component: () => import("@/views/main/system/menu/menu.vue"),
//   },
//   {
//     path: "./main/system/role",
//     component: () => import("@/views/main/system/role/role.vue"),
//   },
//   {
//     path: "/main/product/category",
//     component: () => import("@/views/main/product/category/category.vue"),
//   },
//   {
//     path: "/main/product/goods",
//     component: () => import("@/views/main/product/goods/goods.vue"),
//   },
//   {
//     path: "/main/story/chat",
//     component: () => import("@/views/main/story/chat/chat.vue"),
//   },
//   {
//     path: "/main/story/list",
//     component: () => import("@/views/main/story/list/list.vue"),
//   },
// ];

// 2.动态添加路由

// 路由导航守卫
router.beforeEach((to) => {
  const token = localCache.getCache(LOGIN_TOKEN);
  if (to.path.startsWith("/main") && !token) {
    return "/login";
  }

  // 如果是进入到main
  if (to.path === "/main") {
    return firstMenu?.url;
  }
});

export default router;
